<template>
  <div class="calendar">
    <el-row type="flex" justify="end">
      <!-- 年 -->
      <template>
        <el-select v-model="currentYear" placeholder="年" @change="changeDate">
          <el-option
            v-for="item in yearList"
            :key="item"
            :label="item"
            :value="item"
          >
          </el-option>
        </el-select>
      </template>
      <!-- 月 -->
      <template>
        <el-select v-model="currentMouth" placeholder="月" @change="changeDate">
          <el-option v-for="item in 12" :key="item" :label="item" :value="item">
          </el-option>
        </el-select>
      </template>
    </el-row>
    <el-calendar v-model="currentDate">
      <template slot="dateCell" slot-scope="{ date }">
        <div class="date-content">
          <span class="text"> {{ date.getDate() }}</span>
          <span v-if="isWeek(date)" class="rest"> 休 </span>
        </div>
      </template>
    </el-calendar>
  </div>
</template>

<script>
export default {
  data () {
    return {
      currentDate: new Date(),
      currentYear: null,
      currentMouth: null,
      yearList: [],
      value: ''
    }
  },
  created () {
    let year = this.currentDate.getFullYear()
    this.currentYear = this.currentDate.getFullYear()
    this.currentMouth = this.currentDate.getMonth() + 1
    for (let i = year - 5; i <= year + 5; i++) {
      this.yearList.push(i)
    }
  },
  methods: {
    changeDate () {
      // 让日历跟着改变，根据我选择的年、月来改变日历
      // this.currentYear + this.currentMonth
      this.currentDate = new Date(`${this.currentYear}-${this.currentMonth}-01`)
    },

    isWeek (date) {
      // getDate()获取日（年、月、日里的日）
      // 日期对象有个方法叫 getDay() 获得一周内的第几天
      // 简单来说就是获取星期几，只不过它是从0开始
      // 外国是以星期天为一周的开始，所以星期天为0，星期一就是1，星期2就是2
      // 总而言之就是1-6都是对的，星期天为0
      return date.getDay() == 6 || date.getDay() == 0
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep .el-calendar-day {
  height: auto;
}
::v-deep .el-calendar-table__row td,
::v-deep .el-calendar-table tr td:first-child,
::v-deep .el-calendar-table__row td.prev {
  border: none;
}
.date-content {
  height: 40px;
  text-align: center;
  line-height: 40px;
  font-size: 14px;
}
.date-content .rest {
  color: #fff;
  border-radius: 50%;
  background: rgb(250, 124, 77);
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
  font-size: 12px;
  margin-left: 10px;
}
.date-content .text {
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
}
::v-deep .el-calendar-table td.is-selected .text {
  background: #409eff;
  color: #fff;
  border-radius: 50%;
}
::v-deep .el-calendar__header {
  display: none;
}
::v-deep .el-input__inner {
  height: 30px;
  width: 100px;
  margin-left: 10px;
}
</style>
